<!DOCTYPE html>
<html lang="ch-CN">
<head>
  <meta charset="UTF-8">
  <title>图片切换</title>
</head>
<style>
  body {
    width: 100%;
    height: 100vh;
    background-color: wheat;
    font-size: 30px;
    text-decoration: none;
    font-family: "华文楷体";
  }

  p {
    font-size: 40px;
    margin: 20px 0 0 0;
    font-weight: bold;
  }

  span {
    display: block;
    line-height: 50px;
  }
  a{
    color: fuchsia;
    text-decoration: none;
  }
  a:hover{
    color: skyblue;
  }
  #Img{
    width: 450px;
    height: 200px;
    margin: 0 auto;
    text-align: center;
    position: relative;
  }
  img{
    width: 300px;
    height: 200px;
    border: solid 1px pink;
    transition: linear 0.3s;
  }
  img:hover{
    transform: scale(1.1);
  }
  button{
    font-size: 15px;
    cursor: pointer;
    background-color: transparent;
    border: none;
    transition: linear 0.3s;
  }
  #button1{
    position: absolute;
    left: 3%;
    top: 50%;
  }
  #button2{
    position: absolute;
    top: 50%;
    right: 3%;

  }
  button:hover{
    color: gray;
    transform: scale(1.1);
  }
</style>
<body>
<div id="Img">
  <img src="image/mz1.png" alt="">
  <button id="button1">上一张</button>
  <button id="button2">下一张</button>
</div>
</body>
<script>

  function allButton(button,fun){
    // 获取id
    let btn=document.getElementById(button)
    // 回调函数
    btn.onclick=fun
  }
  var count=0
  var img=["mz1","mz2","mz3","mz4","mz5","mz6","mz7","mz8","mz9","mz10"]
  var image=document.getElementsByTagName("img")[0]

  allButton("button2",function (){
    if (count>=img.length-1){
      count=0
    }else
      count++
    image.src="image/"+img[count]+".png"
    // console.log("image/"+img[count]+".png")
  })
  allButton("button1",function (){
    if (count<=0){
      count=img.length-1
    }else
      count--
    image.src="image/"+img[count]+".png"
    // console.log("image/"+img[count]+".png")
  })
</script>
</html>